# """
# 模型的评估
# """
# import torch
# import numpy as np
#
# import config
# from seq2seq import Seq2seq
#
#
# def evaluate():
#     data = [str(i) for i in np.random.randint(1, 1e8, size=[100])]
#     data = sorted(data, key=lambda x:len(x), reverse=True)
#     target = [i+"0" for i in data]
#     input_length = torch.LongTensor([len(i) for i in data])
#     input = torch.LongTensor([config.num_seq.word_to_num_transform(list(i), config.max_len) for i in data])
#
#     model = Seq2seq()
#     model.load_state_dict(torch.load("./model/train.model"))
#
#     # predict: [12, batch_size]
#     predict = model.evaluate(input, input_length)
#     print(type(predict))  # list
#     print(type(predict[0]))  # numpy.ndarray
#
#     # predict: [batch_size, 12]
#     predict = np.array(predict).T
#
#     result = []
#     for line in predict:
#         temp_result = config.num_seq.num_to_word_transform(line)
#         cur_line = ""
#         for word in temp_result:
#             if word == config.num_seq.EOS_TAG:
#                 break
#
#             cur_line += word
#         result.append(cur_line)
#
#     print(data[:10])
#     print(result[:10])
#
#     print("accuracy:{}".format(sum([1 if i == j else 0 for i, j in zip(target, result)])/len(target)))

"""模型的评估"""
import torch
import numpy as np

import config
from seq2seq import Seq2seq


def evaluate():
    # 1. 准备测试
    test_data = np.random.randint(1, 1e8, size=[1000])
    test_data = [str(i) for i in test_data]
    test_data = sorted(test_data, key=lambda x: len(x), reverse=True)

    # 2. 准备真实目标值
    target = [i+"0" for i in test_data]

    # 3. 数据处理
    input = torch.LongTensor([config.num_seq.word_to_num_transform(list(i), max_len=config.max_len) for i in test_data]).to(config.device)
    input_length = torch.LongTensor([len(i) for i in test_data]).to(config.device)

    # 4. 进行预测
    # 4.1 加载模型
    model = Seq2seq().to(config.device)
    model.load_state_dict(torch.load("./model/train.model"))
    # 4.2 预测
    # predict: [max_len+2, batch_size]
    predict = model.evaluate(input, input_length)
    # predict: [batch_size, max_len+2]
    predict = np.array(predict).T
    print(predict.shape)

    # 5. 测试
    result = []
    for line in predict:
        temp_vector = config.num_seq.num_to_word_transform(line)
        cur_line = ""
        for word in temp_vector:
            if word == config.num_seq.EOS_TAG:
                break

            cur_line += word

        result.append(cur_line)

    print(test_data[:10])
    print(result[:10])

    # zip操作，比如说target=(1, 2), result=(2, 3, 4),list(zip(target, result))后的结果为
    # [(1, 2), (2, 3)],即长度为min(len(target), len(result))
    print(f"accuracy: {sum([1 if i == j else 0 for i, j in zip(target, result)])/len(test_data)}")


if __name__ == '__main__':
    evaluate()


